﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    // Layout = null;
    // var url = Context.Request.GetEncodedPathAndQuery();
    // string d = "";
    //var url = Request.RawUrl;
}
@section css {
    <link rel="stylesheet" href="~/layminiadmin/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all" asp-append-version="true">
    <style>
        .less-query .more {
            display: none;
        }

        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
}


<div class="layuimini-container">
    <div class="layuimini-main"> 
        <div>
            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 5px;">
                    <form class="layui-form layui-form-pane less-query" id="frmSearch" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">关键字</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="keyword" autocomplete="off" class="layui-input">
                                </div>
                            </div> 
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon layui-icon-search"></i> 搜 索</button>
                                <button type="button" class="layui-btn layui-btn-primary" id="btnMoreQuery">
                                    <i class="layui-icon layui-icon-down"></i>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset> 
            <script type="text/html" id="TPL-treeTable-toolbar">
                <div class="layui-btn-container">
                    <button class="layui-btn  layui-btn-normal   layui-btn-xs" id="btn-expand">全部展开</button>
                    <button class="layui-btn layui-btn-normal  layui-btn-xs" id="btn-fold">全部折叠</button>
                     <button class="layui-btn layui-btn-normal layui-btn-xs data-add-btn" lay-event="add"> <i class="layui-icon layui-icon-add-1"></i> 添加 </button>
                </div>
            </script>
            <table id="menuable" class="layui-hide" lay-filter="menu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>
@section js {
    <script>
        var baseUrl = '@(Url.Content("~/"))';
        layui.use(['form', 'treeTable'], function () {
            var $t = layui.jquery;
            var treeTable = layui.treeTable;
            var form = layui.form;
            $("#btnMoreQuery").on("click", function () {
                var t = $(this)
                var frmSearch = $("#frmSearch");
                var icon = t.find('i');
                if (icon.hasClass('layui-icon-up')) {
                    icon.removeClass('layui-icon-up').addClass('layui-icon-down');
                    frmSearch.addClass("less-query").removeClass('more-query');
                } else {
                    icon.addClass('layui-icon-up').removeClass('layui-icon-down');
                    frmSearch.addClass("more-query").removeClass('less-query');
                }
                 $(".table-search-fieldset").trigger("resize");
            });
            // 渲染表格
            var menuTreeTable = treeTable.render({
                elem: '#menuable',
                id: 'menuable',
                toolbar: '#TPL-treeTable-toolbar',
                height: function () {
                    var height = $(".table-search-fieldset").height();
                    // 69 为各个 margin 的和
                    var resultHeight = $(window).height() - height - 35;
                    if (resultHeight < 300) {
                        return 'auto';
                    }
                    return resultHeight;
                }, // 最大高度减去其他容器已占有的高度差
                tree: {
                    customName: {
                        id: 'id',
                        pid: 'parentId',
                        name: 'name'
                    },
                    data: {
                        isSimpleData: true,
                        expandAll: true,
                        rootPid: '0'
                    },
                    view: {
                        expandAllDefault: true
                    }
                },
                method: 'post',
                contentType: 'application/json',
                url: '/sys/menu/AllList',
                page: false,
                cols: [[
                    { type: 'numbers', width: 50 },
                    { field: 'name', width:120, title: '菜单名' },
                    { field: 'icon',width:150,title: '图标', templet: function (d) {
                            // console.log(d);
                            if(d.icon){
                                   return '<span><i class="'+d.icon+'"></i>'+ d.icon+'</span>';
                            }
                            return d.icon;
                        } 
                    },
                    { field: 'permission',width:75,title: '权限标识' },
                    { field: 'url', width:80,title: '菜单url' },
                    { field: 'sort', width: 65, align: 'center', title: '排序号' },
                    {
                        field: 'status', width: 65, align: 'center', title: '状态', templet: function (d) {
                            if (d.status == 0) {
                                return '<span class="layui-badge layui-bg-blue">可用</span>';
                            }
                            if (d.status == 1) {
                                return '<span class="layui-badge">禁用</span>';
                            } else {
                                return d.status;
                            }
                        }
                    },
                    {
                        field: 'menuType', width: 70, align: 'center', title: '类型', templet: function (d) {
                            if (d.menuType == 1) {
                                return '<span class="layui-badge layui-bg-gray">按钮</span>';
                            }
                            if (d.parentId ==0 || !d.parentId ) {
                                return '<span class="layui-badge layui-bg-blue">目录</span>';
                            } else {
                                return '<span class="layui-badge-rim">菜单</span>';
                            }
                        }
                    },
                    { templet: '#auth-state', width: 150, align: 'left', title: '操作' }
                ]],
                parseData: function (res) {
                    // console.log(res);
                    return {
                        "code": res.resultCode, //解析接口状态
                        "msg": res.message, //解析提示文本 
                        "data": res.data //解析数据列表
                    };
                },
                size:'sm',
                done: function () {
                    layer.closeAll('loading');
                },
                autoSort: false, // 服务端排序
                response: {
                    statusCode: 200 // 重新规定成功的状态码为 200，table 组件默认为 0
                }
            });
            $(window).on('resize',function(){
                //console.log('.table-search-fieldset resize:', $(".table-search-fieldset").height(),menuTreeTable);
                treeTable.resize();
            });
            $(".table-search-fieldset").on('resize',function(){
                // console.log('.table-search-fieldset resize:', $(".table-search-fieldset").height(),menuTreeTable);
                treeTable.resize();
            });
            $('#btn-expand').click(function () {
                treeTable.expandAll('menuable', true);
            });

            $('#btn-fold').click(function () {
                // treeTable.foldAll('#menu-table');
                treeTable.expandAll('menuable', false);
            });

            /**
            * toolbar监听事件
            */
             treeTable.on('toolbar(menu-table)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '添加菜单',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '@(Url.Content("~/sys/menu/edit/"))',
                        end: function(e){

                          // 紧紧重新加载数据
                          treeTable.reloadData('menuable');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'delete') {  // 监听删除操作
                     var checkStatus = table.checkStatus(obj.config.id)
                            , data = checkStatus.data;
                    if(!data || data.length == 0 ){
                        layer.msg('请选择需要删除的数据！');
                        return ;
                    }
                    layer.confirm('真的删除行么', function (index) {
                       // BatDelete
                        var ids =  data.map(d=>d.id).join(",")
                         $.ajax({
                                url:  '@(Url.Content("~/sys/menu/BatDelete"))',
                                data: {ids : ids},
                                method:'post',
                                success: function (d) {
                                    layer.close(index);
                                    if (d.resultCode == "200") {
                                        treeTable.reload('menuable');
                                    } else {
                                        layer.msg(d.message);
                                        layer.close(index);
                                    }
                                },
                                error: function (e) {
                                    layer.close(index);
                                }
                            });
                    });
                }
            });

            //监听工具条
            treeTable.on('tool(menu-table)', function (obj) {
               var data = obj.data;
                // console.log('数据', data, obj);
                if (obj.event === 'edit') {

                    var index = layer.open({
                        title: '编辑菜单',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '@(Url.Content("~/sys/menu/edit/"))' + data.id,
                        end: function(e){
                          //console.log('关闭编辑');
                          // 紧紧重新加载数据
                           treeTable.reload('menuable');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    layer.confirm('真的删除行么', function (index) {
                        obj.del();
                         $.ajax({
                            url:  '@(Url.Content("~/sys/menu/delete/"))' + data.id,
                            data: data,
                            method:'post',
                            success: function (d) {
                                // console.log(d);
                                // l.destroy();
                                layer.close(index);
                                if (d.resultCode == "200") {
                                    treeTable.reload('menuable');
                                } else {
                                    layer.msg(d.message);
                                    layer.close(index);
                                }
                                // alert(d)
                            },
                            error: function (e) {
                                // console.log(e);
                                layer.close(index);
                            }
                        });

                    });
                }
            });

            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                
                // 获取配置项
                var $option = treeTable.getOptions('menuable');
                // console.log('获取配置项', $option);
                // 复制搜索条件
                var where = JSON.parse(JSON.stringify(data.field));
                if ($option.where && $option.where.SortField) {
                    where.SortField = $option.where.SortField
                    where.SortType = $option.where.SortType
                }
                //执行搜索重载
                treeTable.reload('menuable', {
                    where: where
                }, 'data');

                return false;
            });
        });
    </script>
}